package com.siemens.industrial.web.mvc.service;

import com.siemens.industrial.model.IUser;
import com.siemens.industrial.model.admin.Tenant;
import com.siemens.industrial.model.admin.User;
import com.siemens.industrial.security.AuthenticationService;
import com.siemens.industrial.web.mvc.dao.admin.TenantDao;
import com.siemens.industrial.web.mvc.dao.admin.UserDao;
import com.siemens.industrial.web.mvc.dao.partner.PartnerUserDao;
import com.siemens.industrial.web.mvc.service.admin.TenantService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.security.auth.login.CredentialException;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by David on 2016/10/21.
 */
@Service
public class AuthService extends AuthenticationService {
    @Resource
    PasswordEncoder passwordEncoder;

    @Resource
    UserDao userDao;

    @Resource
    PartnerUserDao partnerUserDao;

    @Resource
    TenantDao tenantDao;

    @Resource
    TenantService tenantService;

    AuthService(HttpServletRequest request) {
        super(request);
    }

    public Map<String, Object> attempt(String username, String password, String tenant) {
        if (username == null || username.equals("")) {
            return new HashMap<String, Object>() {{
                put("isSuccess", false);
                put("code", 0);
            }};
        }
        if (password == null || password.equals("")) {
            return new HashMap<String, Object>() {{
                put("isSuccess", false);
                put("code", 1);
            }};
        }
        return checkCredential(username, password, tenant);
    }

    @Override
    public Map<String, Object> checkCredential(String username, String password, String tenant) {
        IUser user;
        if (tenant == null) {
            user = userDao.findOneByName(username);
        } else {
            Tenant ten = tenantDao.findOneByKey(tenant);
            if (ten == null) {
                return new HashMap<String, Object>() {{
                    put("isSuccess", false);
                    put("code", 4);
                }};
            }
            String tenantId = ten.getTenantId();
            user = partnerUserDao.getUserByName(tenant, tenantId, username);
        }

        if (user == null || !passwordEncoder.matches(password, user.getPassword())) {
            return new HashMap<String, Object>() {{
                put("isSuccess", false);
                put("code", 2);
            }};
        }
        if (user.getEnabled() != 1) {
            return new HashMap<String, Object>() {{
                put("isSuccess", false);
                put("code", 3);
            }};
        }

        this.login(user);

        if (user.getIsFirstLogin()==0&&user.getTenant()!=null){
            return new HashMap<String, Object>() {{
                put("isSuccess", true);
                put("firstLogin",true);
            }};
        }

        return new HashMap<String, Object>() {{
            put("isSuccess", true);
        }};
    }

    public String buildMessage(String code) {
        String message = "";
        if (code != null) {
            switch (Integer.parseInt(code)) {
                case 0:
                    message = "用户名不能为空";
                    break;
                case 1:
                    message = "密码不能为空";
                    break;
                case 2:
                    message = "您输入的用户名或密码不正确";
                    break;
                case 3:
                    message = "您的账号已经被禁用,请联系管理员";
                    break;
                case 4:
                    message = "您的登录地址不正确";
                    break;
            }
        }
        return message;
    }

    public void updateFirstLogin(String tenant,String userId) {
        String tenantId = tenantService.getTenantByKey(tenant).getTenantId();
        partnerUserDao.updateUserFirstLogin(tenantId,userId);
    }
}
